<?php

namespace App\Http\Controllers\Home;

use App\Models\Card;
use App\Models\Member;
use App\Models\Room;
use Cookie;

class RoomController extends BaseController
{
    public function __construct(card $card, Member $member, Room $room)
    {
        parent::__construct();
        $this->card = $card;
        $this->member = $member;
        $this->room = $room;
    }


    public function getNum($role, $rid, $mid)
    {
//        if (!($role && $rid && $mid)) {
//            return redirect(action('Home\IndexController@getLogin'));
//        }
//        if ($this->mid != $mid) {
//            return redirect(action('Home\IndexController@getLogin'));
//        }
        $data['room'] = $this->room->getRoomData($rid,$mid);
//        debug($data);
//        $data['join_info'] = ['role' => $role, 'rid' => $rid, 'mid' => $mid, 'type' => 1];
//        return view('home.wait', compact('data', 'joinInfo'));
        return ajax_return($data);
    }


    public function getPlay($role, $rid, $mid)
    {
        if (!($role && $rid && $mid)) {
            return redirect(action('Home\IndexController@getLogin'));
        }
        if ($this->mid != $mid) {
            return redirect(action('Home\IndexController@getLogin'));
        }
        $data = $this->room->getRoomData($rid);
//        debug($data);
        $joinInfo = ['role' => $role, 'rid' => $rid, 'mid' => $mid, 'type' => 3];
        debug($data);
        $left = $data['players'][$data['order']['left']];
        $right = $data['players'][$data['order']['right']];
        $my = $data['players'][$data['order']['footer']];
        return view('home.game', compact('data', 'joinInfo', 'left', 'right', 'my'));
    }


    public function getJoin()
    {
        $mid = request('mid');
        $pwd = request('pwd');
        $res = $this->room->join($mid, $pwd);
        return response()->json($res);
    }


    public function getCreate($mid)
    {
        $res = $this->room->createRoom($mid,request('round'));
        return response()->json($res);
//        if (!$res) {
//            return redirect(action('Home\MemberController@getIndex'));
//        }
//        return redirect(action('Home\RoomController@getNum',['id'=>$res]));
    }


    public function getJoinGame($id)
    {
        if (!$id) {
            abort(404);
        }
        $room = $this->room->getOne(['id' => $id]);
//        $mroom = $this->room->getRoom($id);
//        dd($mroom);
        return view('home.room', compact('id'));
    }


    public function getTest()
    {

//        a 胡牌的基本牌型：11、123、123、123、123
//        b 胡牌的基本牌型：11、123、123、123、111
//        c 胡牌的基本牌型：11、123、123、111、111
//        d 胡牌的基本牌型：11、123、111、111、111
//        e 胡牌的基本牌型：11、111、111、111、111
//        f 胡牌的基本牌型：11、11、11、11、11、11、11


//        $ids = [1,2,6,7,45,46,54,55,69,70,75,76,83,84];
//        $cards = "";
//        $cardsCache = $this->card->getAllCardsCache();
//        foreach ($ids as $v) {
//            $cards .= $cardsCache[$v-1]['cate'];
//        }
//        var_dump($cards);
        //基本胡型
        $pa = "/(aa|bb|cc|dd|ee|ff|gg|hh|ii|jj|kk|ll|mm|nn|oo|pp|qq|rr|ss|tt|uu){1}(abc|bcd|cde|def|efg|fgh|ghi|jkl|klm|lmn|mno|nop|opq|pqr|stu){4}$/";
        $pb = "/(aa|bb|cc|dd|ee|ff|gg|hh|ii|jj|kk|ll|mm|nn|oo|pp|qq|rr|ss|tt|uu){1}(abc|bcd|cde|def|efg|fgh|ghi|jkl|klm|lmn|mno|nop|opq|pqr|stu){3}(aaa|bbb|ccc|ddd|eee|fff|ggg|hhh|iii|jjj|kkk|lll|mmm|nnn|ooo|ppp|qqq|rrr|sss|ttt|uuu){1}$/";
        $pc = "/(aa|bb|cc|dd|ee|ff|gg|hh|ii|jj|kk|ll|mm|nn|oo|pp|qq|rr|ss|tt|uu){1}(abc|bcd|cde|def|efg|fgh|ghi|jkl|klm|lmn|mno|nop|opq|pqr|stu){2}(aaa|bbb|ccc|ddd|eee|fff|ggg|hhh|iii|jjj|kkk|lll|mmm|nnn|ooo|ppp|qqq|rrr|sss|ttt|uuu){2}$/";
        $pd = "/(aa|bb|cc|dd|ee|ff|gg|hh|ii|jj|kk|ll|mm|nn|oo|pp|qq|rr|ss|tt|uu){1}(abc|bcd|cde|def|efg|fgh|ghi|jkl|klm|lmn|mno|nop|opq|pqr|stu){1}(aaa|bbb|ccc|ddd|eee|fff|ggg|hhh|iii|jjj|kkk|lll|mmm|nnn|ooo|ppp|qqq|rrr|sss|ttt|uuu){3}$/";

        //七对
        $pf = "/(aa|bb|cc|dd|ee|ff|gg|hh|ii|jj|kk|ll|mm|nn|oo|pp|qq|rr|ss|tt|uu){7}$/";
        $pf1 = "/(aaaa|bbbb|cccc|dddd|eeee|ffff|gggg|hhhh|iiii|jjjj|kkkk|llll|mmmm|nnnn|oooo|pppp|qqqq|rrrr|ssss|tttt|uuuu){1}(aa|bb|cc|dd|ee|ff|gg|hh|ii|jj|kk|ll|mm|nn|oo|pp|qq|rr|ss|tt|uu){5}$/";
        $pf2 = "/(aa|bb|cc|dd|ee|ff|gg|hh|ii|jj|kk|ll|mm|nn|oo|pp|qq|rr|ss|tt|uu){1}(aaaa|bbbb|cccc|dddd|eeee|ffff|gggg|hhhh|iiii|jjjj|kkkk|llll|mmmm|nnnn|oooo|pppp|qqqq|rrrr|ssss|tttt|uuuu){1}(aa|bb|cc|dd|ee|ff|gg|hh|ii|jj|kk|ll|mm|nn|oo|pp|qq|rr|ss|tt|uu){4}$/";
        $pf3 = "/(aa|bb|cc|dd|ee|ff|gg|hh|ii|jj|kk|ll|mm|nn|oo|pp|qq|rr|ss|tt|uu){2}(aaaa|bbbb|cccc|dddd|eeee|ffff|gggg|hhhh|iiii|jjjj|kkkk|llll|mmmm|nnnn|oooo|pppp|qqqq|rrrr|ssss|tttt|uuuu){1}(aa|bb|cc|dd|ee|ff|gg|hh|ii|jj|kk|ll|mm|nn|oo|pp|qq|rr|ss|tt|uu){3}$/";
        $pf4 = "/(aa|bb|cc|dd|ee|ff|gg|hh|ii|jj|kk|ll|mm|nn|oo|pp|qq|rr|ss|tt|uu){3}(aaaa|bbbb|cccc|dddd|eeee|ffff|gggg|hhhh|iiii|jjjj|kkkk|llll|mmmm|nnnn|oooo|pppp|qqqq|rrrr|ssss|tttt|uuuu){1}(aa|bb|cc|dd|ee|ff|gg|hh|ii|jj|kk|ll|mm|nn|oo|pp|qq|rr|ss|tt|uu){2}$/";
        $pf5 = "/(aa|bb|cc|dd|ee|ff|gg|hh|ii|jj|kk|ll|mm|nn|oo|pp|qq|rr|ss|tt|uu){4}(aaaa|bbbb|cccc|dddd|eeee|ffff|gggg|hhhh|iiii|jjjj|kkkk|llll|mmmm|nnnn|oooo|pppp|qqqq|rrrr|ssss|tttt|uuuu){1}(aa|bb|cc|dd|ee|ff|gg|hh|ii|jj|kk|ll|mm|nn|oo|pp|qq|rr|ss|tt|uu){1}$/";
        $pf6 = "/(aa|bb|cc|dd|ee|ff|gg|hh|ii|jj|kk|ll|mm|nn|oo|pp|qq|rr|ss|tt|uu){5}(aaaa|bbbb|cccc|dddd|eeee|ffff|gggg|hhhh|iiii|jjjj|kkkk|llll|mmmm|nnnn|oooo|pppp|qqqq|rrrr|ssss|tttt|uuuu){1}$/";
        //卡五星
        $pg = "/(deeef|mnnno|mnoqq|defhh){1}$/";
        $ph = "/(aa|bb|cc|dd|ee|ff|gg|hh|ii|jj|kk|ll|mm|nn|oo|pp|qq|rr|ss|tt|uu){1}(deeffg|mnnoop){1}$/";
        //碰碰胡
        $pe = "/(aa|bb|cc|dd|ee|ff|gg|hh|ii|jj|kk|ll|mm|nn|oo|pp|qq|rr|ss|tt|uu){1}(aaa|bbb|ccc|ddd|eee|fff|ggg|hhh|iii|jjj|kkk|lll|mmm|nnn|ooo|ppp|qqq|rrr|sss|ttt|uuu){4}$/";
        //大三元
        $pj = "/(aa|bb|cc|dd|ee|ff|gg|hh|ii|jj|kk|ll|mm|nn|oo|pp|qq|rr){1}(abc|bcd|cde|def|efg|fgh|ghi|jkl|klm|lmn|mno|nop|opq|pqr){1}(ssstttuuu){1}$/";
        $pj1 = "/(aa|bb|cc|dd|ee|ff|gg|hh|ii|jj|kk|ll|mm|nn|oo|pp|qq|rr){1}(aaa|bbb|ccc|ddd|eee|fff|ggg|hhh|iii|jjj|kkk|lll|mmm|nnn|ooo|ppp|qqq|rrr){1}(ssstttuuu){1}$/";
        $pj2 = "/(aa|bb|cc|dd|ee|ff|gg|hh|ii|jj|kk|ll|mm|nn|oo|pp|qq|rr){1}(abc|bcd|cde|def|efg|fgh|ghi|jkl|klm|lmn|mno|nop|opq|pqr){1}(ssssttttuuuu){1}$/";
        $pj3 = "/(aa|bb|cc|dd|ee|ff|gg|hh|ii|jj|kk|ll|mm|nn|oo|pp|qq|rr){1}(aaa|bbb|ccc|ddd|eee|fff|ggg|hhh|iii|jjj|kkk|lll|mmm|nnn|ooo|ppp|qqq|rrr){1}(ssssttttuuuu){1}$/";
        //小三元
        $pj4 = "/(ss|tt|uu){1}(abc|bcd|cde|def|efg|fgh|ghi|jkl|klm|lmn|mno|nop|opq|pqr){2}(sss|ttt|uuu){2}$/";
        $pj5 = "/(ss|tt|uu){1}(aaa|bbb|ccc|ddd|eee|fff|ggg|hhh|iii|jjj|kkk|lll|mmm|nnn|ooo|ppp|qqq|rrr){2}(sss|ttt|uuu){2}$/";
        $pj6 = "/(ss|tt|uu){1}(abc|bcd|cde|def|efg|fgh|ghi|jkl|klm|lmn|mno|nop|opq|pqr){2}(ssss|tttt|uuuu){2}$/";
        $pj7 = "/(ss|tt|uu){1}(aaa|bbb|ccc|ddd|eee|fff|ggg|hhh|iii|jjj|kkk|lll|mmm|nnn|ooo|ppp|qqq|rrr){2}(ssss|tttt|uuuu){2}$/";

        $ca = 'ssabcdefhijklm';
        $cb = 'ssabcdefhijkkk';
        $cc = 'ssabcdefhhhkkk';
        $cd = 'ssabcdddhhhkkk';
        $ce = 'ddcccpppeeemmm';
        $cf = 'ccppeammddqquu';
        $cg = 'deeef';
        $ch = 'iideeffg';


        $str = 'aabbccddeeff';
        $preg = '//';
        preg_match($pd, $cd, $matches);
        dd($matches);
    }


    public function getHu()
    {
//aaabcdkklmopqt
//        dd($cards);

        $cards1 = [0,
            3, 1, 1, 1, 0, 0, 0, 0, 0,         //   PAI[1-9]     壹条～玖条的个数
            0,
            0, 2, 1, 1, 0, 1, 1, 1, 0,         //   PAI[11-19]   壹铜～玖铜的个数
            0,
            0, 1, 0                      //  PAI[21-23]    中发白的个数
        ];
        $cards = [0,
            0, 0, 0, 0, 0, 1, 1, 1, 0,
            0,
            0, 0, 0, 1, 1, 3, 0, 0, 0,
            0,
            0, 0, 0
        ];
        $cards2 = [0,
            1, 1, 1, 0, 1, 1, 1, 0, 0,         //   PAI[1-9]     壹万～玖万的个数
            0,
            0, 0, 0, 0, 0, 3, 0, 0, 0,         //   PAI[11-19]     壹铜～玖铜的个数
            0,
            0, 2, 0, 3, 0, 0, 0, 0, 0,         //   PAI[21-29]     壹条～玖条的个数
            0,
            0, 0, 0, 0, 0, 0, 0              //   PAI[31-37]     东南西北中发白的个数
        ];
        //   请务必先排除“七对”   和“十三幺”，由于简单，所以不提供了
        //   if(   QIDUI(PAI)   )...
        //   if(   SHISANYAO(PAI)   )...
        $jiang = 0;
        $arr1 = '';
        $res = hu($cards1,$jiang,$arr1);
//        $res = checkLiang($cards1, $jiang, $arr1);
        dp($res);
        if ($res) {
            echo("哈！我胡啦！\n");
        } else {
            echo("哎，胡不成！\n");
        }
    }

    public function getH()
    {
        $cards = [0,
            0, 0, 0, 0, 0, 1, 1, 1, 0,
            0,
            0, 0, 0, 1, 1, 3, 0, 0, 0,
            0,
            0, 0, 0
        ];
        $jiang = 0;
        $temCards = $cards;
        $arr = '';
        $res = checkGang($cards,$arr);
        debug($res);
        $res = checkKe($res['cards'],$res['data']);
        debug($res);
        $res = checkDui($jiang, $res['cards'],$res['data']);
        debug($res);
        $res = checkShun($res['cards'],$res['data']);
        debug($res);
        debug($res['data']);
        return dp($res);
//

//        $res = hu4($cards);
//        if ($res) {
//            dp($res);
//            return '胡啦';
//        }
//        return '胡不了';

    }

    public function getL()
    {
        $cards = [0,
            0, 0, 0, 0, 0, 1, 1, 0, 0,
            0,
            0, 0, 0, 0, 2, 3, 0, 0, 0,
            0,
            0, 0, 0
        ];
        $res = checkLiang2($cards);
        dd($res);
    }


}
